import java.util.ArrayDeque;
import java.util.Deque;

public class Sort {


    //直接插入排序
    public static void insetsort(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(arr[j] > tmp){
                    arr[j+1] = arr[j];
                }else{
                    arr[j+1] = tmp;
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }

    //希尔排序
    public static void shellsort(int[] arr){
        int gap = arr.length;
        while(gap > 1){
            gap /= 2;
            shell(arr,gap);
        }
    }

    private static void shell(int[] arr,int gap){
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if(arr[j] > tmp){
                    arr[j+gap] = arr[j];
                }else{
                    arr[j+gap] = tmp;
                    break;
                }
            }
            arr[j+gap] = tmp;
        }
    }

    private static void swap(int[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    //选择排序 和数据是否有序无关
    //时间复杂度：O(n^2)  空间复杂度O(1) 不稳定
    public static void Selectsort(int[] arr){
        int minindex = 0;
        for (int i = 0; i < arr.length; i++) {
            minindex = i;
            for (int j = i+1; j < arr.length; j++) {
                if(arr[j] < arr[minindex]){
                    minindex = j;
                }
            }
            swap(arr,minindex,i);
        }
    }

    //选择排序的优化
    public static void SelectsortBetter(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int minindex = left;
            int maxindex = left;
            for (int i = left + 1; i < right; i++) {
                if(arr[i] < arr[minindex]){
                    minindex = i;
                }
                if(arr[i] > arr[maxindex]){
                    maxindex = i;
                }
            }
            swap(arr,minindex,left);
            //最大值刚好是本轮循环的left下标，此时最大值被换到了minindex位置
            if(maxindex == left){
                maxindex = minindex;
            }
            swap(arr,maxindex,right);
            left++;
            right--;
        }
    }

    //堆排序
    public static void heapSort(int[] arr){
        createSort(arr);
        int end = arr.length - 1;
        while(end > 0){
            swap(arr,end,0);
            shiftDown(arr,0,end);
            end--;
        }
    }
    public static  void createSort(int[] arr){
        for (int parent = (arr.length-1-1)/2; parent >= 0; parent--) {
            shiftDown(arr,parent,arr.length);
        }
    }
    //向下调整
    private static void shiftDown(int[] arr,int parent,int length){
        int child = parent * 2 + 1;
        while(child < length){
            if(child+1 < length && arr[child + 1] > arr[child]){
                child++;
            }
            if(arr[child] > arr[parent]){
                swap(arr,parent,child);
                parent = child;
                child = parent*2 +1;
            }else
            {
                break;
            }
        }
    }


    //冒泡排序
    //时间复杂度O（n） 空间复杂度O(1) 稳定性：稳定
    //两次优化：1.-i 减少遍历次数 2.定义flag用于剩下元素以及有序的情况
    public static void BubbleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j] > arr[j+1]){
                    swap(arr,j,j+1);
                    flag = true;
                }
            }
            if(flag == false){
                break;
            }
        }
    }


    //快速排序
    public static void quickSort(int[] arr){
        //quick(arr,0,arr.length-1);
        quickNor(arr,0,arr.length-1);
    }
    //非递归写法
    public static void quickNor(int[] arr,int start,int end){
        Deque<Integer> stack = new ArrayDeque<>();
        int povid = partitionHoare(arr,start,end);
        if(povid > start + 1){
            stack.push(start);
            stack.push(povid - 1);
        }
        if(povid < end - 1){
            stack.push(povid + 1);
            stack.push(end);
        }

        while(!stack.isEmpty()){
            end = stack.pop();
            start = stack.pop();
            povid = partitionHoare(arr,start,end);
            if(povid > start + 1){
                stack.push(start);
                stack.push(povid - 1);
            }
            if(povid < end - 1){
                stack.push(povid + 1);
                stack.push(end);
            }
        }
    }
    //递归写法
    private static void quick(int[] arr,int start,int end){
        //递归停止条件
        if(start >= end){
            return;
        }

        //优化：递归到后面每个小数组基本有序，使用直接插入排序，更加优化
        if(end - start +1 <= 10){
            insertRange(arr,start,end);
            return;
        }
        //优化:三数取中法(让数据变得更加平均分布，避免单分支二叉树递归的情况出现)
        int midindex = getMiddleNum(arr,start,end);
        swap(arr,midindex,start);

        int povid = partitionHoare(arr, start, end);
        //int povid = partition(arr, start, end);
        quick(arr,start,povid-1);
        quick(arr,povid+1,end);
    }

    private static void insertRange(int[] arr,int start,int end){
        for (int i = 1+start; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if(arr[j] > tmp){
                    arr[j+1] = arr[j];
                }else{
                    arr[j+1] = tmp;
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }

    private static int getMiddleNum(int[] arr,int left,int right){
        int mid = (left + right)/2;
        if(arr[left] > arr[right]){
            if(arr[mid] > arr[left]){
                return left;
            }else if(arr[mid] < arr[right]){
                return right;
            }else {
                return mid;
            }
        }else{
            if(arr[mid] > arr[right]){
                return right;
            }else if(arr[mid] < arr[left]){
                return left;
            }else {
                return mid;
            }
        }
    }

    //挖坑法
    private  static int partition(int[] arr,int left,int right){
        int key = arr[left];
        while(left < right){
            while(left < right && arr[right] >= key){
                right--;
            }
            arr[left] = arr[right];
            while(left < right && arr[left] <= key){
                left++;
            }
            arr[right] = arr[left];
        }

        arr[left] = key;
        return left;
    }
    //Hoare法
    private static int partitionHoare(int[] arr,int left,int right){
        int tmp = arr[left];
        int tmpindex = left;
        while(left < right){
            while(left < right && arr[right] >= tmp){
                right--;
            }
            while(left < right && arr[left] <= tmp){
                left++;
            }
            swap(arr,left,right);
        }
        swap(arr,tmpindex,left);

        return left;
    }


    //归并排序
    public static void mergeSort(int[] arr){
        mergeSortmp(arr,0,arr.length-1);
    }

    private static void mergeSortmp(int[] arr,int left,int right){
        //递归返回条件
        if(left >= right){
            return;
        }

        int mid = (left + right)/2;

        mergeSortmp(arr,left,mid);
        mergeSortmp(arr,mid+1,right);

        //合并
        merge(arr,left,mid,right);
    }

    private static void merge(int[] arr,int left,int mid,int right){
        int[] tmp = new int[right-left+1];
        int k = 0;
        int s1 = left;
        //int e1 = mid;
        int s2 = mid+1;
        //int e2 = right;
        while (s1 <= mid && s2 <= right) {
            if(arr[s1] <= arr[s2]) {
                tmp[k++] = arr[s1++];
            }else {
                tmp[k++] = arr[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[k++] = arr[s1++];
        }
        while (s2 <= right) {
            tmp[k++] = arr[s2++];
        }
        //可以保证tmp数组 是有序的
        for (int i = 0; i < k; i++) {
            arr[i+left] = tmp[i];
        }
    }

    //非递归
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }
}
